#!/bin/bash

echo "设置项目权限..."

# 创建必要的目录
mkdir -p pgbackrest-keys scripts
mkdir -p /var/log/pgbackrest 2>/dev/null || true
mkdir -p /backup/pgbackrest/archive 2>/dev/null || true
mkdir -p /backup/pgbackrest/backup 2>/dev/null || true

# 设置脚本执行权限
chmod +x scripts/*.sh 2>/dev/null || true
chmod +x setup-permissions.sh
chmod 777 /var/log/pgbackrest 2>/dev/null || true
chmod -R 777 /backup 2>/dev/null || true

# 确保配置文件存在
touch pgbackrest-unified.conf
touch postgresql.conf

echo "权限设置完成!"
echo ""
echo "===== PostgreSQL备份与恢复配置向导 ====="
echo ""
echo "项目已经更新为统一配置模式，使用单一配置文件支持服务器A和B。"
echo ""
echo "使用说明："
echo "1. 确保.env文件中设置正确的SERVER_TYPE值"
echo "   - 在服务器A上设置: SERVER_TYPE=A"
echo "   - 在服务器B上设置: SERVER_TYPE=B"
echo ""
echo "2. 启动服务"
echo "   docker compose up -d"
echo ""
echo "3. 执行备份(在服务器A上)"
echo "   docker exec pgbackrest-A /scripts/init-pgbackrest-enhanced.sh"
echo ""
echo "4. 将备份文件传输到服务器B"
echo "   scp /backup/pgbackrest/archive/pgbackup_*.tar.gz 服务器B地址:/backup/pgbackrest/archive/"
echo ""
echo "5. 解压备份文件(在服务器B上)"
echo "   docker exec pgbackrest-B bash -c \"mkdir -p /backup/pgbackrest/backup && tar -xzf /backup/pgbackrest/archive/pgbackup_*.tar.gz -C /backup/pgbackrest\""
echo ""
echo "6. 恢复备份(在服务器B上)"
echo "   docker exec pgbackrest-B pgbackrest --stanza poddb --log-level-console info restore"
echo ""
echo "注意: 请确保Docker Compose版本 ≥ 2.0.0"
echo "检查版本命令: docker compose version"
echo ""
echo "如需完整说明，请参阅README.md文件。"
